<template>
  <el-dialog v-model="dialogFormVisible" :title="title" width="800">
    <el-form :model="form" label-width="auto" style="max-width: 800px" :rules="rules">
      <div class="form-scroll">
        <el-row :gutter="20">
          <template v-for="(item, index) in Object.keys(form)">
            <template v-if="item !== 'list' && form[item].type !== 'textarea'">
              <el-col :span="12">
                <el-form-item :label="item" :prop="item">
                  <template v-if="form[item].type === 'datetime'">
                    <el-date-picker
                      v-model="form[item].value"
                      type="date"
                      :placeholder="`请选择${item}`"
                      value-format="YYYY-MM-DD"
                    />
                  </template>
                  <template v-else-if="form[item].type === 'select'">
                    <template v-if="item === '任务地点'">
                      <el-select v-model="form[item].value" placeholder="请选择任务地点">
                        <el-option label="渝北区龙兴镇-饮水库" value="渝北区龙兴镇-饮水库"></el-option>
                        <el-option label="渝北区木耳镇-污水厂" value="渝北区木耳镇-污水厂"></el-option>
                      </el-select>
                    </template>
                  </template>
                  <template v-else>
                    <el-input v-model="form[item].value" :placeholder="`请填写${item}`" />
                  </template>
                </el-form-item>
              </el-col>
            </template>
            <template v-else>
              <template v-if="form[item].type === 'textarea'">
                <el-col :span="24">
                  <el-form-item :label="item" :prop="item">
                    <el-input type="textarea" v-model="form[item].value" :placeholder="`请填写${item}`" />
                  </el-form-item>
                </el-col>
              </template>
            </template>
          </template>
        </el-row>
      </div>
    </el-form>
    <div>
      <el-button type="primary" @click="onSubmit">{{ isEdit? '编辑': '新增' }}</el-button>
      <el-button @click="dialogFormVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
// 方法
import { ref, defineExpose } from 'vue'

const instance = getCurrentInstance();
const { proxy } = instance || {};

// 显示表单
const dialogFormVisible = ref(false)
const title = ref('新增任务')
const isEdit = ref(false)
// 表单
const form = ref({
  '任务标题': { type: '', value: '' },
  '任务地点': { type: 'select', value: '' },
  '开始时间': { type: 'datetime', value: '' },
  '预计完成时间': { type: 'datetime', value: '' },
  '任务要求': { type: 'textarea', value: '' },
  '备注': { type: 'textarea', value: '' },
})
// 验证
const rules = ref({
  '任务标题': [{ required: true, message: '请输入任务标题', trigger: 'blur' },],
  '任务地点': [{ required: true, message: '请选择任务地点', trigger: 'blur' },],
  '开始时间': [{ required: true, message: '请选择开始时间', trigger: 'blur' },],
  '预计完成时间': [{ required: true, message: '请选择预计完成时间', trigger: 'blur' },],
  '任务要求': [{ required: true, message: '请输入任务要求', trigger: 'blur' },],
  '备注': [{ required: true, message: '请输入备注', trigger: 'blur' },],
})

// 打开方法
const open = () => {
  for (let i in form.value) {
    form.value[i].value = ''
  }
  dialogFormVisible.value = true
  isEdit.value = false
  title.value = '新增任务'
}
// 编辑
const edit = (data) => {
  for (let i in form.value) {
    form.value[i].value = data[i]
  }
  isEdit.value = true
  dialogFormVisible.value = true
  title.value = '编辑任务'
}
// 关闭方法
const close = () => {
  dialogFormVisible.value = false
}

// 提交
const onSubmit = () => {
  for (let i in form.value) {
    if (!form.value[i].value) {
      if (form.value[i].type === 'select' || form.value[i].type === 'datetime') {
        proxy.$modal.msgWarning('请选择' + i);
      } else {
        proxy.$modal.msgWarning('请t填写' + i);
      }
      return
    }
  }
  proxy.$modal.msgSuccess(title.value + '成功');
  close()
}

defineExpose({
  open, close, edit
})

</script>

<style scoped lang="scss">

</style>
